Enumeration, Class, Structure

DataStructureTest.playground

// Enumeration & Class & Structure: Playground – noun: a place where people can play
// 2015.09.21 Kyoung Shin Park @ Dankook University

import Cocoa

func sayHello(personName: String = “World”) -> String {
let greeting = “Hello, ” + personName + “!”
return greeting
}
print(sayHello())
print(sayHello(“Anna”))

// Enumeration
enum Gender {
case Female
case Male
init() { // initialization
self = .Female
}
init(_ name: String)
{
switch name {
case “Female”, “여자”: self = .Female
case “Male”, “남자”: self = .Male
default: self = .Female
}
}
var description: String {
switch self {
case .Female: return “FEMALE~”
case .Male: return “MALE~”
}
}
}
var gender = Gender()
print(gender.description)
gender = Gender(“남자”)
print(gender.description)
gender = Gender.Female
gender = .Male
print(Gender.Male.description)

switch gender {
case .Female: print(“FEMALE!!”)
case .Male: print(“MALE!!”)
}

// Enum, multiple member values can appear on a single line, separated by commas
// Enum rawValue
enum Planet: Int {
case Mercury=1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
let earthID = Planet.Earth.rawValue
let somePlanet = Planet.Earth
switch somePlanet {
case .Earth:
print(“Mostly harmless”)
default:
print(“Not a safe place for humans”)
}
let aPlanet = Planet(rawValue: 7)
switch aPlanet! {
case .Earth:
print(“Mostly harmless”)
default:
print(“Not a safe place for humans”)
}
if let possiblePlanet = Planet(rawValue: 9) {
switch possiblePlanet {
case .Earth:
print(“Mostly harmless”)
default:
print(“Not a safe place for humans”)
}
} else {
print(“There isn’t a planet at position 9”)
}

// Enum associated values
enum TrainStatus {
case OnTime
case Delayed(Int)
}
var status:TrainStatus = .Delayed(5)
status = .OnTime
switch status {
case .OnTime:
print(“Train is on time”)
case .Delayed(let minutes):
print(“Train is delayed by \(minutes) minutes”)
}
// class
class Vehicle {
// stored properties
var numberOfPassengers: Int = 2
var numberOfWheels: Int = 4

// computed properties
var NumberOfWheels: Int {
get { return numberOfWheels }
set { numberOfWheels = newValue }
}
var description: String {
return “\(numberOfWheels) number of wheels”
}
}

class Bicycle: Vehicle {
override init() {
super.init()
numberOfPassengers = 1
numberOfWheels = 2
}
}

class Car: Vehicle {
// stored properties
var minVelocity: Int = 30
var accelVelocity: Int = 10
// computed properties
var speed: Int {
get {
return minVelocity + accelVelocity
}
set(newVelocity) {
accelVelocity = newVelocity – minVelocity
}
}
override init() {
super.init()
numberOfWheels = 5
}
override var description: String {
return “\(numberOfWheels) number of wheels with \(speed) speeds”
}
}
let aVehicle = Vehicle()
print(aVehicle.description)
aVehicle.numberOfWheels = 6
print(aVehicle.description)
let aBike = Bicycle()
print(aBike.description)
let aCar = Car()
print(aCar.description)
// structure
struct Frame {
var x: Int, y: Int // stored properties
var width: Int, height: Int // stored properties
var area: Int { // computed properties (getter)
return width * height
}
mutating func addWidth (width: Int) {
self.width += width // mutating modifies the value of a stored property in struct
}
}

let f = Frame(x:5, y:10, width:100, height:100)
print(f.width)
//f.width = 250 // invalid (due to let)

var g = Frame(x:5, y:10, width:100, height:100)
g.addWidth(15) // use mutating func to modify the value of a stored property in struct
print(g.width)

let h = Frame(x:5, y:10, width:100, height:100)
//h.addWidth(15) // compiler error (can’t call mutating func of struct stored in a let)
print(h.width)

Leave a Reply

Your email address will not be published. Required fields are marked *